package com.desktopmng.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import com.desktopmng.core.support.Pager;
import com.desktopmng.exception.JFossException;
import com.desktopmng.orm.UserToDstc;

@SuppressWarnings({"unchecked","rawtypes"})
public class UserToDstcManager extends HibernateEntityDao<UserToDstc> {
	/**
	 * Description: 新增用户与桌面内容关联
	 * 
	 * @param userToDstc
	 *            用户与桌面内容关联对象
	 * @return 操作标记
	 * @throws JFossException
	 */
	public String addUserToDstc(UserToDstc userToDstc) throws JFossException {
		String retStr = "true";
		try {
			userToDstc.setId(null);
			this.getHibernateTemplate().save(userToDstc);
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}

	/**
	 * Description: 修改用户与桌面内容关联
	 * 
	 * @param userToDstc
	 *            用户与桌面内容关联对象
	 * @return 操作标记
	 * @throws JFossException
	 */
	public String updateUserToDstc(UserToDstc userToDstc) throws JFossException {
		String retStr = "true";
		try {
			this.getHibernateTemplate().update(userToDstc);
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}

	/**
	 * Description: 删除用户与桌面内容关联
	 * 
	 * @param userToDstcId
	 *            用户与桌面内容关联对象Id
	 * @return 操作标记
	 * @throws JFossException
	 */
	public String deleteUserToDstc(Long userId, Long dstcId)
			throws JFossException {
		String retStr = "true";
		try {
			Properties prop = new Properties();
			prop.setProperty("userId", userId.toString());
			prop.setProperty("dstcId", dstcId.toString());
			List paramList;
			paramList = this.findUserToDstcByCondition(prop);
			if (!paramList.isEmpty()) {

				UserToDstc dst = (UserToDstc) paramList.get(0);
				System.out.println(dst.getDstcId());
				this.getHibernateTemplate().delete(dst);

			}
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}

	/**
	 * Description: 查看用户与桌面内容关联对象
	 * 
	 * @param userToDstcId
	 *            用户与桌面内容关联对象Id
	 * @return 用户与桌面内容关联对象
	 * @throws JFossException
	 */
	public UserToDstc findUserToDstcById(Long userToDstcId)
			throws JFossException {
		UserToDstc obj = null;
		try {
			if (null != userToDstcId) {
				obj = (UserToDstc) this.getHibernateTemplate().load(
						UserToDstc.class, userToDstcId.longValue());
			} else {
				return null;
			}

		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return obj;
	}

	/**
	 * Description: 条件查询用户与桌面内容关联库
	 * 
	 * 条件参数列表，为空时，返回全部用户与桌面内容关联列表
	 * 
	 * @param prop
	 *            条件参数表
	 * @return 封装用户与桌面内容关联库对象的列表
	 * @throws JFossException
	 */
	public List findUserToDstcByCondition(Properties prop)
			throws JFossException {
		List list = null;
		try {

			String hql = "select p from UserToDstc p where 1=1";
			List paramList = new ArrayList();
			if (null != prop && prop.size() > 0) {
				String userId = prop.getProperty("userId");
				if (StringUtils.isNotBlank(userId)) {
					hql += " and p.userId = ?";
					paramList.add(new Long(userId));
				}
			}
			if (null != prop && prop.size() > 0) {
				String dstcId = prop.getProperty("dstcId");
				if (StringUtils.isNotBlank(dstcId)) {
					hql += " and p.dstcId = ?";
					paramList.add(new Long(dstcId));
				}
			}
			hql += " order by p.id";
			list = this.find(hql, paramList.toArray());

		} catch (Exception e) {

			throw new JFossException("", e);
		}
		if (!list.isEmpty()) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * Description: 根据查询条件获取用户与桌面内容关联库对象列表,分页查询
	 * 
	 * @param prop
	 *            查询条件
	 * @param pageNum
	 *            页码
	 * @param pageSize
	 *            页数据量
	 * @return 用户与桌面内容关联库对象列表分页对象
	 * @throws JFossException
	 */
	public Pager findUserToDstcByCondition(Properties prop, int pageNum,
			int pageSize) throws JFossException {
		Pager page = null;
		try {

			String hql = "select p from UserToDstc p where 1=1";
			List paramList = new ArrayList();
			if (null != prop && prop.size() > 0) {
				String msStatus = prop.getProperty("userId");
				if (StringUtils.isNotBlank(msStatus)) {
					hql += " and p.userId = ?";
					paramList.add(msStatus);
				}
			}
			hql += " order by p.id";
			page = this.pagedQuery(hql, pageNum, pageSize, paramList.toArray());

		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return page;
	}
	/**
	 *  Description: 根据roleIdID和dstcId已经存在的查询
	 * 
	 * 
	 */
	public List findDstcByroleIdBydstcId(Properties prop) throws JFossException {
		List list = null;

		try {

			String hql = "select p from DstcToRole p where 1=1";
			List paramList = new ArrayList();

			if (null != prop && prop.size() > 0) {
				String dstcId = prop.getProperty("dstcIds");
				if (StringUtils.isNotBlank(dstcId)) {
					hql += "and p.dstcId not in (" + dstcId.trim() + ")";
				}
			}
			if (null != prop && prop.size() > 0) {
				String userId = prop.getProperty("roleId");
				if (StringUtils.isNotBlank(userId)) {
					hql += " and p.roleId = ?";
					paramList.add(new Long(userId));
				}
			}
			hql += " order by p.id";
			list = this.find(hql, paramList.toArray());

		} catch (Exception e) {

			throw new JFossException("", e);
		}
		if (!list.isEmpty()) {
			return list;
		} else {
			return null;
		}
	}
	
	/**
	 * 根据条件查询个人桌面内容，组装成布局模板信息
	 * @param prop
	 * @return
	 * @throws JFossException
	 */
	public String getTemplateByCondition(Properties prop) throws JFossException {
		List userToDstcList = this.findUserToDstcByCondition(prop);
		String template = "";
		//组装个人桌面布局信息
		if (userToDstcList != null && userToDstcList.size() > 0) {
			for (int i = 0; i < userToDstcList.size(); i++) {
				UserToDstc dstc = (UserToDstc) userToDstcList.get(i);
				template = dstc.getDstcId().toString() + ",";
			}
			template += "&";
		}
		return template;
	}
}
